﻿<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        SetConnectionString();
        InitializeActiveRecord();
        InitializeProjectConfiguration();
        InitializeUsersConfiguration();
    }

    private void InitializeUsersConfiguration()
    {
        ApplicationGenerator.CommonModule.UserManager.FilePath = System.IO.Path.Combine(HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath), "Users.txt");
        ApplicationGenerator.CommonModule.UserManager.GetUsers();
    }
    
    private void InitializeProjectConfiguration()
    {
        if (System.Web.Configuration.WebConfigurationManager.AppSettings["ProjectPath"] == null)
        {
            throw new Exception("Brak skonfigurowanego \"ProjectPath\" w pliku konfiguracyjnym");
        }

        ApplicationGenerator.CommonModule.ProjectConfiguration.ProjectDirectoryPath =
            System.Web.Configuration.WebConfigurationManager.AppSettings["ProjectPath"];

        ApplicationGenerator.CommonModule.ProjectConfiguration.SetUpProjectConfiguration();
    }
    
    private void InitializeActiveRecord()
    {
        const string binDirectoryName = "Bin";
        string applicationServerPath = HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath);
        ApplicationGenerator.BusinessModule.MetaElements.MetaModelManager metaManager = ApplicationGenerator.BusinessModule.MetaElements.MetaModelManager.Instance;

        string pathToAssembly = System.IO.Path.Combine(
            applicationServerPath,
            System.IO.Path.Combine(binDirectoryName, ApplicationGenerator.CommonModule.ActiveRecordConfiguration.AssemblyName));

        if (!System.IO.File.Exists(pathToAssembly))
        {
            throw new System.IO.FileNotFoundException(pathToAssembly);
        }
        
        System.Reflection.Assembly modelsAssembly = System.Reflection.Assembly.LoadFile(pathToAssembly);
        System.Reflection.AssemblyName assemblyName = modelsAssembly.GetName();
        System.Reflection.Assembly assembly = AppDomain.CurrentDomain.Load(assemblyName);

        Castle.ActiveRecord.Framework.Config.InPlaceConfigurationSource source = new Castle.ActiveRecord.Framework.Config.InPlaceConfigurationSource();
        source.Add(typeof(Castle.ActiveRecord.ActiveRecordBase), 
            ApplicationGenerator.CommonModule.ActiveRecordConfiguration.GetConfiguration(
            System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ApplicationGeneratorDatabase"].ConnectionString            
            ));
        
        try
        {
            Castle.ActiveRecord.ActiveRecordStarter.Initialize(assembly, source);
        }
        catch (Exception ex)
        {
            throw ex;
        }        
    }
    
    private void SetConnectionString()
    {
        if (System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ApplicationGeneratorDatabase"] == null
            || string.IsNullOrEmpty(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ApplicationGeneratorDatabase"].ConnectionString))
        {
            throw new Exception("W konfiguracji musi znajdować się ConnectionString nazwany ApplicationGeneratorDatabase");
        }

        ApplicationGenerator.DataAccess.DesignerDataBaseManager.Instance.ConnectionString
            = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ApplicationGeneratorDatabase"].ConnectionString;
    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown
        Castle.ActiveRecord.ActiveRecordStarter.ResetInitializationFlag();
    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }
       
</script>
